package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPizzaDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.FastClickUtil;
import stark.common.bean.StkResBean;

public class PizzaDetailActivity extends BaseAc<ActivityPizzaDetailBinding> {

    public static StkResBean stkResBean;
    private boolean isCollect=false;

    @Override
    protected int onCreate() {
        return R.layout.activity_pizza_detail;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPizzaDetailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPizzaCollect.setOnClickListener(this);
        Glide.with(mContext).load(stkResBean.getThumbUrl()).into(mDataBinding.ivPizzaDetailImg);
        mDataBinding.tvPizzaDetailName.setText(stkResBean.getName());
        mDataBinding.tvPizzaDetailDosing.setText(stkResBean.getDesc());
        mDataBinding.webView.loadUrl(stkResBean.getUrl());
    }

    @Override
    protected void onClickCallback(View v) {
        if (FastClickUtil.isFastClick()) return;
        switch (v.getId()){
            case R.id.ivPizzaCollect:
                if (isCollect){
                    cancelCollect();
                }else {
                    addCollect();
                }
                isCollect();
                break;
        }
    }

    @Override
    protected void initData() {
        isCollect();
    }

    private void isCollect(){
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            for (StkResBean resBean : listSp) {
                if (resBean.equals(stkResBean)){
                    isCollect=true;
                    mDataBinding.ivPizzaCollect.setImageResource(R.drawable.ayishoucang);
                }else {
                    isCollect=false;
                    mDataBinding.ivPizzaCollect.setImageResource(R.drawable.aweishoucang);
                }
            }
        } else {
            isCollect=false;
            mDataBinding.ivPizzaCollect.setImageResource(R.drawable.aweishoucang);
        }
    }

    private void addCollect(){
        List<StkResBean> listSave = new ArrayList<>();
        listSave.add(stkResBean);
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.saveCollectList(listSp);
        } else {
            AppSpUtils.saveCollectList(listSave);
        }
    }

    private void cancelCollect(){
        List<StkResBean> listSp = AppSpUtils.getCollectList();
        listSp.remove(stkResBean);
        AppSpUtils.saveCollectList(listSp);
    }


}